<?php
// +----------------------------------------------------------------------
// | 数据库迁移 - 填充初始数据
// +----------------------------------------------------------------------

use think\facade\Db;
use think\migration\Migrator;

class InitialData extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        Db::name('system_users')->insert([
            'username'    => 'admin',
            'nickname'    => '管理员',
            'password'    => password_hash('admin@123456', PASSWORD_DEFAULT),
            'phone'       => '13700000000',
            'email'       => 'admin@facker.com',
            'remark'      => '由系统自动创建的管理员账号',
            'sort'        => 99,
            'status'      => 1,
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
        ]);
        Db::name('system_users')->insert([
            'username'    => 'common',
            'nickname'    => '普通用户',
            'password'    => password_hash('admin@123456', PASSWORD_DEFAULT),
            'phone'       => '13800000000',
            'email'       => 'common@facker.com',
            'remark'      => '由系统自动创建的普通用户账号',
            'sort'        => 99,
            'status'      => 1,
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
        ]);

        Db::name('system_roles')->json(['permissions'])->insert([
            'role_name'   => '超级管理员',
            'code'        => 'admin',
            'permissions'  => [
                "System",
                "Testing",
                "Template",
                "SystemUser",
                "SystemRole",
                "SystemMenu",
                "SystemDept",
                "TestingPermission",
                "TestingAdmin",
                "TestingComponent",
                "backend:btn:add",
                "backend:btn:edit",
                "backend:btn:delete",
                "TestingAbout",
                "TemplateTable"
            ],
            'sort'        => 99,
            'remark'      => '由系统自动创建的角色',
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
        ]);

        Db::name('system_roles')->json(['permissions'])->insert([
            'role_name'   => '普通管理员',
            'code'        => 'common',
            'permissions'  => [
                "Testing",
                "TestingComponent",
                "backend:btn:add",
                "backend:btn:edit",
                "TestingPermission",
                "TestingCommon",
                "TestingAbout"
            ],
            'sort'        => 99,
            'remark'      => '由系统自动创建的角色',
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
        ]);

        Db::name('system_users_roles')->insert([
            'user_id'     => 1,
            'role_id'     => 1,
        ]);

        Db::name('system_users_roles')->insert([
            'user_id'     => 2,
            'role_id'     => 2,
        ]);

        $menus = [
            [
                "id" => 25,
                "parent_id" => 2,
                "type" => "menu",
                "menu_name" => "组件测试",
                "route_name" => "TestingComponent",
                "path" => "/testing/component",
                "component" => "testing/component",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "",
                "options" => 11001,
                "remark" => "",
                "sort" => 5,
                "status" => true
            ],
            [
                "id" => 111,
                "parent_id" => 0,
                "type" => "menu",
                "menu_name" => "常规模板",
                "route_name" => "Template",
                "path" => "/template",
                "component" => "",
                "frame_src" => "",
                "redirect" => "/template/table",
                "icon" => "ep:message-box",
                "options" => 11001,
                "remark" => "",
                "sort" => 5,
                "status" => true
            ],
            [
                "id" => 21,
                "parent_id" => 2,
                "type" => "menu",
                "menu_name" => "权限测试",
                "route_name" => "TestingPermission",
                "path" => "/testing/permission",
                "component" => "testing/permission",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "",
                "options" => 11001,
                "remark" => "",
                "sort" => 10,
                "status" => true
            ],
            [
                "id" => 5,
                "parent_id" => 21,
                "type" => "button",
                "menu_name" => "添加",
                "route_name" => "backend:btn:add",
                "path" => "",
                "component" => "",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "",
                "options" => 11001,
                "remark" => "",
                "sort" => 10,
                "status" => true
            ],
            [
                "id" => 1,
                "parent_id" => 0,
                "type" => "menu",
                "menu_name" => "系统管理",
                "route_name" => "System",
                "path" => "/system",
                "component" => "",
                "frame_src" => "",
                "redirect" => "/system/user",
                "icon" => "ri:settings-3-line",
                "options" => 11001,
                "remark" => "",
                "sort" => 10,
                "status" => true
            ],
            [
                "id" => 11,
                "parent_id" => 111,
                "type" => "menu",
                "menu_name" => "表格模板",
                "route_name" => "TemplateTable",
                "path" => "/template/table",
                "component" => "template/table/index",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "ri:table-line",
                "options" => 11001,
                "remark" => "",
                "sort" => 10,
                "status" => true
            ],
            [
                "id" => 6,
                "parent_id" => 21,
                "type" => "button",
                "menu_name" => "修改",
                "route_name" => "backend:btn:edit",
                "path" => "",
                "component" => "",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "",
                "options" => 11001,
                "remark" => "",
                "sort" => 20,
                "status" => true
            ],
            [
                "id" => 22,
                "parent_id" => 2,
                "type" => "menu",
                "menu_name" => "admin权限",
                "route_name" => "TestingAdmin",
                "path" => "/testing/admin",
                "component" => "testing/admin",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "",
                "options" => 11001,
                "remark" => "",
                "sort" => 20,
                "status" => true
            ],
            [
                "id" => 10,
                "parent_id" => 1,
                "type" => "menu",
                "menu_name" => "用户管理",
                "route_name" => "SystemUser",
                "path" => "/system/user",
                "component" => "system/user/index",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "ri:admin-line",
                "options" => 11001,
                "remark" => "",
                "sort" => 20,
                "status" => true
            ],
            [
                "id" => 12,
                "parent_id" => 1,
                "type" => "menu",
                "menu_name" => "角色管理",
                "route_name" => "SystemRole",
                "path" => "/system/role",
                "component" => "system/role/index",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "ri:admin-fill",
                "options" => 11001,
                "remark" => "",
                "sort" => 20,
                "status" => true
            ],
            [
                "id" => 7,
                "parent_id" => 21,
                "type" => "button",
                "menu_name" => "删除",
                "route_name" => "backend:btn:delete",
                "path" => "",
                "component" => "",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "",
                "options" => 11001,
                "remark" => "",
                "sort" => 30,
                "status" => true
            ],
            [
                "id" => 24,
                "parent_id" => 2,
                "type" => "menu",
                "menu_name" => "common权限",
                "route_name" => "TestingCommon",
                "path" => "/testing/common",
                "component" => "testing/common",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "",
                "options" => 11001,
                "remark" => "",
                "sort" => 30,
                "status" => true
            ],
            [
                "id" => 13,
                "parent_id" => 1,
                "type" => "menu",
                "menu_name" => "菜单管理",
                "route_name" => "SystemMenu",
                "path" => "/system/menu",
                "component" => "system/menu/index",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "ep:menu",
                "options" => 11001,
                "remark" => "",
                "sort" => 30,
                "status" => true
            ],
            [
                "id" => 23,
                "parent_id" => 2,
                "type" => "menu",
                "menu_name" => "关于",
                "route_name" => "TestingAbout",
                "path" => "/testing/about",
                "component" => "testing/about",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "",
                "options" => 11001,
                "remark" => "",
                "sort" => 40,
                "status" => true
            ],
            [
                "id" => 14,
                "parent_id" => 1,
                "type" => "menu",
                "menu_name" => "部门管理",
                "route_name" => "SystemDept",
                "path" => "/system/dept",
                "component" => "system/dept/index",
                "frame_src" => "",
                "redirect" => "",
                "icon" => "ri:git-branch-line",
                "options" => 11001,
                "remark" => "",
                "sort" => 40,
                "status" => true
            ],
            [
                "id" => 2,
                "parent_id" => 0,
                "type" => "menu",
                "menu_name" => "测试路由",
                "route_name" => "Testing",
                "path" => "/testing",
                "component" => "",
                "frame_src" => "",
                "redirect" => "/testing/permission",
                "icon" => "ri:test-tube-line",
                "options" => 11001,
                "remark" => "",
                "sort" => 100,
                "status" => true
            ]
        ];

        foreach ($menus as $menu) {
            Db::name('system_menus')->insert(array_merge($menu, [
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ]));
        }

        Db::name('system_users_messages')->insert([
            'user_id'     => 1,
            'title'       => '安全通知',
            'description' => '系统检测到异常登录，请立即验证',
            'is_read'     => true,
            'create_time' => (new DateTime())->modify('-15 day')->format('Y-m-d H:i:s'),
            'update_time' => (new DateTime())->modify('-15 day')->format('Y-m-d H:i:s'),
        ]);

        Db::name('system_users_messages')->insert([
            'user_id'     => 1,
            'title'       => '系统提醒',
            'description' => '您提交的工单已受理，客服将于24小时内回复',
            'is_read'     => true,
            'create_time' => (new DateTime())->modify('-5 day')->format('Y-m-d H:i:s'),
            'update_time' => (new DateTime())->modify('-5 day')->format('Y-m-d H:i:s'),
        ]);

        Db::name('system_users_messages')->insert([
            'user_id'     => 1,
            'title'       => '安全通知',
            'description' => '账户安全设置已更新，请确认是否为您本人操作',
            'is_read'     => false,
            'create_time' => (new DateTime())->modify('-1 day')->format('Y-m-d H:i:s'),
            'update_time' => (new DateTime())->modify('-1 day')->format('Y-m-d H:i:s'),
        ]);

        Db::name('system_users_messages')->insert([
            'user_id'     => 1,
            'title'       => '系统提醒',
            'description' => '您有1条待处理审批，请及时查看',
            'is_read'     => false,
            'create_time' => (new DateTime())->format('Y-m-d H:i:s'),
            'update_time' => (new DateTime())->format('Y-m-d H:i:s'),
        ]);

        Db::name('template_table_catalog')->insert([
            'name'        => '测试分类一',
            'remark'      => '',
            'status'      => true,
            'sort'        => 99,
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
        ]);

        Db::name('template_table_catalog')->insert([
            'name'        => '测试分类二',
            'remark'      => '',
            'status'      => true,
            'sort'        => 99,
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
        ]);
    }
}
